//
//  ZDRightServiceController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/14.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDRightServiceController.h"
#import "ZDServiceCompanyCell.h"
#import "ZDStoreModel.h"

#import "ZDStoreDetialController.h"

static NSString *const ZDServiceCompanyCellId = @"ZDServiceCompanyCell";

@interface ZDRightServiceController ()
/**
 <#Description#>
 */
@property(nonatomic,copy)NSString *gcId;
/**
 <#Description#>
 */
@property(nonatomic,copy)NSString *distance;

@end

@implementation ZDRightServiceController

- (void)setupUI
{
    [super setupUI];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDServiceCompanyCell" bundle:nil] forCellReuseIdentifier:ZDServiceCompanyCellId];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectDistance:) name:SELECT_DISTANCE_NOTIFICATION object:nil];
}
- (void)loadData
{
    self.distance = @"10000";
}
- (void)viewWillAppear:(BOOL)animated
{
    [self resetTablewFrame:self.view.bounds];
}
#pragma mark- 网络请求

/**
 1.郑州市lat = 34.749081,long = 113.561026
 2.威海市lat = 37.394438,long = 122.703477
 3.沧州市lat = 38.299216,long = 116.806101
 4.徐州市lat = 34.293952,long = 117.142654
 */
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.gcId forKey:@"gc_id"];
    [dict setObject:self.distance forKey:@"distance"];

    CGFloat lat = [[NSUserDefaults standardUserDefaults] floatForKey:LOCATION_LAT];
    CGFloat longs = [[NSUserDefaults standardUserDefaults] floatForKey:LOCATION_LONG];
    if (lat>34.293952 && lat<38.299216)
    {
        if (longs>113.561026 && longs<122.703477)
        {
            [dict setObject:@(lat) forKey:@"lat"];
            [dict setObject:@(longs) forKey:@"lng"];
        }else
        {
            [dict setObject:@"36.67937" forKey:@"lat"];
            [dict setObject:@"117.133311" forKey:@"lng"];
        }
    }else{
    
        [dict setObject:@"36.67937" forKey:@"lat"];
        [dict setObject:@"117.133311" forKey:@"lng"];
    }
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API netAccess:ServiceStoreListUrl andMethor:@"GET" andDict:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        if (error)
        {
            [weakSelf addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [weakSelf hiddenNoDataView];
            [weakSelf.dataSource removeAllObjects];
            
            NSArray *tempt = datas[@"store_list"];
            NSArray *tempModel = [ZDStoreModel mj_objectArrayWithKeyValuesArray:tempt];
            if (tempModel.count > 0)
            {
                [weakSelf hiddenNoDataView];
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }else
            {
                [weakSelf addNoDataViewWithTipText:@"您周围没有该服务"];
            }

        }
        [weakSelf.tableView reloadData];
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDServiceCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDServiceCompanyCellId forIndexPath:indexPath];
    if (self.dataSource.count > 0)
    {
        cell.model = self.dataSource[indexPath.row];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDStoreModel *storeModel = self.dataSource[indexPath.row];
    ZDStoreDetialController *storeDetial = [[ZDStoreDetialController alloc] init];
    storeDetial.storeId = storeModel.store_id;
    storeDetial.storeName = storeModel.store_name;
    [self.navigationController pushViewController:storeDetial animated:YES];
}

#pragma mark- leftCategoryClickDelegate
- (void)categoryControllerSelected:(NSString *)gcId
{
    //加载数据去
    if ([_gcId isEqualToString:gcId]) {
        return;
    }
    _gcId = gcId;
    [self requestRefreshData];

}
- (void)selectDistance:(NSNotification *)notification
{
    NSIndexPath *indexPath = notification.object;
    if (indexPath.row == 0) {
        self.distance = @"1";
    }else if (indexPath.row == 1){
        self.distance = @"3";
    }else if (indexPath.row == 2){
        self.distance = @"5";
    }else if (indexPath.row == 3){
        self.distance = @"8";
    }else if (indexPath.row == 4){
        self.distance = @"10000";
    }else if (indexPath.row == 10){
        ZDLog(@"%s---%d---点击了空白",__func__,__LINE__);
        return;
    }
    [self requestRefreshData];
}

@end



